package cn.iocoder.yudao.module.train.dal.mysql.coursestudentregistform;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.CourseClassesPageReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseStudentRegistFormDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.course.CourseDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.train.dal.dataobject.venue.VenueDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform.vo.*;

/**
 * 班级学生 Mapper
 *
 * @author waynelam
 */
@Mapper
public interface CourseStudentRegistFormMapper extends BaseMapperX<CourseStudentRegistFormDO> {

    default PageResult<CourseStudentRegistFormDTO> selectPage(CourseStudentRegistFormPageReqVO reqVO) {
        Page<CourseStudentRegistFormDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        MPJLambdaWrapper<CourseStudentRegistFormDO> wrapper = new MPJLambdaWrapper<CourseStudentRegistFormDO>()
                .selectAll(CourseStudentRegistFormDO.class)
                .select(StudentDO::getStudentName)
                .leftJoin(StudentDO.class, StudentDO::getId, CourseStudentRegistFormDO::getStudentId)
                .selectAs(CourseDO::getName, CourseStudentRegistFormDTO::getCourseName)
                .leftJoin(CourseClassesDO.class, CourseClassesDO::getId, CourseStudentRegistFormDO::getClassesId)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .eqIfExists(CourseStudentRegistFormDO::getClassesId, reqVO.getClassesId())
                .eqIfExists(CourseStudentRegistFormDO::getDuration, reqVO.getDuration())
                .eqIfExists(CourseStudentRegistFormDO::getRestDuration, reqVO.getRestDuration())
                .likeIfExists(StudentDO::getStudentName, reqVO.getStudentName());
        IPage<CourseStudentRegistFormDTO> resultPage = selectJoinPage(page, CourseStudentRegistFormDTO.class, wrapper);
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default Long selectStudentCount(Long classesId) {
        return selectCount(CourseStudentRegistFormDO::getStudentId, classesId);
    }

    default List<CourseStudentRegistFormDTO> selectListByClassesId(Long classesId) {
        return selectJoinList(CourseStudentRegistFormDTO.class, new MPJLambdaWrapper<CourseStudentRegistFormDO>()
                .selectAll(CourseStudentRegistFormDO.class)
                .select(StudentDO::getStudentName)
                .leftJoin(StudentDO.class, StudentDO::getId, CourseStudentRegistFormDO::getStudentId)
                .selectAs(CourseDO::getName, CourseStudentRegistFormDTO::getCourseName)
                .leftJoin(CourseClassesDO.class, CourseClassesDO::getId, CourseStudentRegistFormDO::getClassesId)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .eqIfExists(CourseStudentRegistFormDO::getClassesId, classesId));
    }

    default List<CourseStudentRegistFormDTO> selectListByStudentId(Long studentId) {
        return selectJoinList(CourseStudentRegistFormDTO.class, new MPJLambdaWrapper<CourseStudentRegistFormDO>()
                .selectAll(CourseStudentRegistFormDO.class)
                .select(StudentDO::getStudentName)
                .leftJoin(StudentDO.class, StudentDO::getId, CourseStudentRegistFormDO::getStudentId)
                .selectAs(CourseDO::getName, CourseStudentRegistFormDTO::getCourseName)
                .leftJoin(CourseClassesDO.class, CourseClassesDO::getId, CourseStudentRegistFormDO::getClassesId)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .eqIfExists(CourseStudentRegistFormDO::getStudentId, studentId));
    }
}
